package com.dzl.goods;

import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.goods.pojo.WelfareGoodsLabel;
import com.dzl.goods.pojo.dto.WelfareGoodsLabelRelDto;
import com.dzl.goods.pojo.query.WelfareGoodsLabelQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsLabelVo;
import com.dzl.goods.service.WelfareGoodsLabelRelService;
import com.dzl.goods.service.WelfareGoodsLabelService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品标签管理 - 控制层
 * @author WangJian
 * @date 2021/8/2 0002
 */
@RestController
@RequestMapping("/welfareGoodsLabel")
public class WelfareGoodsLabelController extends BaseController {

    @Autowired
    private WelfareGoodsLabelService service;
    @Autowired
    private WelfareGoodsLabelRelService welfareGoodsLabelRelService;

    /**
     * 获取商品标签列表
     * @date 2021/8/2 0002 10:38
     * @author wangjian
     * @param query 标签查询条件
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.goods.pojo.vo.WelfareGoodsLabelVo>>
     */
    @GetMapping("/getGoodsLabelList")
    public PageUtils<WelfareGoodsLabelVo> getGoodsLabelList(WelfareGoodsLabelQuery query){
        PageInfo<WelfareGoodsLabelVo> pageInfo = service.queryGoodsLabelListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 保存商品标签
     * @date 2021/8/2 0002 10:46
     * @author wangjian
     * @param welfareGoodsLabel 标签信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/saveGoodsLabel")
    public JsonResult<Integer> saveGoodsLabel(@RequestBody WelfareGoodsLabel welfareGoodsLabel){
        if (EmptyUtils.isEmpty(welfareGoodsLabel.getName())){
            return JsonResult.failWithMsg("商品标签不能为空");
        }
        if (welfareGoodsLabel.getSort() == null){
            return JsonResult.failWithMsg("排序不能为空");
        }
        if (EmptyUtils.isEmpty(welfareGoodsLabel.getId())){
            welfareGoodsLabel.setStatus(CommonConstants.YES);
            welfareGoodsLabel.setCreaterName(getAdminInfo().getUserName());
        }else {
            welfareGoodsLabel.setUpdaterName(getAdminInfo().getUserName());
        }
        return JsonResult.success(service.updateGoodsLabel(welfareGoodsLabel));
    }

    /**
     * 删除标签
     * @date 2021/8/2 0002 11:15
     * @author wangjian
     * @param id 标签ID
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @DeleteMapping("/deleteGoodsLabel")
    public JsonResult<Integer> deleteGoodsLabel(Integer id){
        if (EmptyUtils.isEmpty(id)){
            return JsonResult.failWithMsg("传入标签ID为空");
        }
        return JsonResult.success(service.deleteGoodsLabel(id));
    }

    /**
     * 删除标签关联商品
     * @date 2022/01/19 10:10
     * @author liuguanjun
     * @param labelId 标签ID
     * @param goodsIds 商品ID
     */
    @DeleteMapping("/deleteGoodsLabelRel")
    public JsonResult<Integer> deleteGoodsLabel(Integer labelId, String goodsIds){
        if(EmptyUtils.isEmpty(labelId)){
            return JsonResult.paramErrWithMsg("标签id不能传空");
        }
        if(EmptyUtils.isEmpty(goodsIds)){
            return JsonResult.paramErrWithMsg("请选择商品");
        }
        return JsonResult.success(welfareGoodsLabelRelService.deleteGoodsLabelRel(labelId, goodsIds));
    }


    /**
     * 保存商品和标签的关联关系
     * @date 2021/8/2 0002 14:58
     * @author wangjian
     * @param dto 商品和标签的关联关系
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/updateGoodsAndLabelList")
    public JsonResult<Integer> updateGoodsAndLabelList(@RequestBody WelfareGoodsLabelRelDto dto){
        if (EmptyUtils.isEmpty(dto.getLabelId())){
            return JsonResult.failWithMsg("传入标签ID为空");
        }
        return JsonResult.success(welfareGoodsLabelRelService.updateGoodsLabelRelList(dto.getLabelId(),dto.getGoodsIds()));
    }

    /**
     * 标签置顶
     * @param id 标签ID
     * @author liuguanjun
     * @date 2021/12/17 16:18
     */
    @GetMapping("/updateGoodsLabelToTop")
    public JsonResult<Integer> updateGoodsLabelToTop(Integer id){
        if (EmptyUtils.isEmpty(id)){
            return JsonResult.failWithMsg("传入标签ID为空");
        }
        return JsonResult.success(service.updateGoodsLabelToTop(id));
    }

    /**
     * 标签取消置顶
     * @param id 标签ID
     * @author liuguanjun
     * @date 2021/12/17 16:20
     */
    @GetMapping("/updateGoodsLabelCancelTop")
    public JsonResult<Integer> updateGoodsLabelCancelTop(Integer id){
        if (EmptyUtils.isEmpty(id)){
            return JsonResult.failWithMsg("传入标签ID为空");
        }
        return JsonResult.success(service.updateGoodsLabelCancelTop(id));
    }
}
